﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace Zx.JsonV
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbSource.Text.Trim()))
                return;
            try
            {
                treeViewer.Items.Clear();
                var json = JToken.ReadFrom(new JsonTextReader(new StringReader(tbSource.Text)));
                var item = new TreeViewItem();
                item.Header = ToTree.Convert.GetTree(json, item);
                treeViewer.Items.Add(item);
                tabControl1.SelectedIndex = 1;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message,"JSON Parse Error",MessageBoxButton.OK);
            }
        }

        private void btnFormat_Click(object sender, RoutedEventArgs e)
        {
            treeViewer.Items.Clear();
            var json = JToken.ReadFrom(new JsonTextReader(new StringReader(tbSource.Text)));
            tbSource.Text = JsonConvert.SerializeObject(json, Formatting.Indented);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tbSource.Text = JsonConvert.SerializeObject(treeViewer, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling=ReferenceLoopHandling.Ignore });
        }

        private void treeViewer_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var treeViewer = sender as TreeView;
            if (treeViewer.SelectedItem == null)
                return;
            subTreeViewer.Items.Clear();
            var item = new TreeViewItem();
            var selected=((TreeViewItem)treeViewer.SelectedItem).Tag as JToken;
            if (selected == null)
                return;
            item.Header = ToTree.Convert.GetTree(selected, item);
            subTreeViewer.Items.Add(item);
            textBox1.Text = JsonConvert.SerializeObject(selected, Formatting.Indented);
        }

        private void subTreeViewer_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var treeViewer = sender as TreeView;
            if (treeViewer.SelectedItem == null)
                return;
            var selected = ((TreeViewItem)treeViewer.SelectedItem).Tag as JToken;
            if (selected == null)
                return;
            textBox1.Text = JsonConvert.SerializeObject(selected, Formatting.Indented);
        }
    }
}
